package hrms.util.amq;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class TopicSubscriber {
	
	public String user;
	
	public String mes;
	
	public TopicSubscriber(String user) {
		this.user = user;
		init();
	}
	
	public void init() {
		ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
		Connection connection;
		try {
			connection = factory.createConnection();
			connection.start();
			
			Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			Topic topic = session.createTopic("Aikes.Topic");
	
			MessageConsumer consumer = session.createConsumer(topic);
			
			System.out.println(user+"启动监听!");
			consumer.setMessageListener(new MessageListener() {
				public void onMessage(Message message) {
					TextMessage tm = (TextMessage) message;
					try {
						
						mes = tm.getText();
						System.out.println(user+"Received message: " + tm.getText());
					} catch (JMSException e) {
						e.printStackTrace();
					}
				}
			});
			
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
//		TopicSubscriber t1 = new TopicSubscriber("订阅者一");
//		TopicSubscriber t2 = new TopicSubscriber("订阅者二");
	}
}